package com.leyou.cart.service;

import com.leyou.cart.pojo.Cart;
import com.leyou.common.auth.pojo.UserHolder;
import com.leyou.common.auth.pojo.UserInfo;
import com.leyou.common.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 登录后的购物车业务
 */
@Service
public class CartService {
    //购物车格式： < userId,  Map<skuId,Cart对象的json字符串>  >
    @Autowired
    private StringRedisTemplate redisTemplate;

    public void addCart(Cart cart) {
        //1. 获取当前登录用户
        BoundHashOperations<String, Object, Object> boundHashOps = getLoginUserCart();

        //获取当前添加商品的skuId
        String skuId = cart.getSkuId().toString();

        //3. 判断当前登录用户的购物车中是否存在添加的商品
       if(boundHashOps.hasKey(skuId)){
           //3.1 存在，则修改该商品的数量

           //新数量= 原数量+添加的数量

           //获取Cart对象的json字符串
           Object o = boundHashOps.get(skuId);

           //把json字符串转换为Cart对象
           Cart oldCart = JsonUtils.toBean(o.toString(),Cart.class);

           //修改商品数量
           cart.setNum(oldCart.getNum()+cart.getNum());
       }

       //3.2 不存在，则在购物车中添加新的商品
       boundHashOps.put(skuId,JsonUtils.toString(cart));
    }

    public List<Cart> loadCarts() {
        BoundHashOperations<String, Object, Object> boundHashOps = getLoginUserCart();

        //3.获取购物车的所有Cart对象
        //values(): 取出hash中的value值
        List<Cart> carts = boundHashOps
                                .values()
                                .stream()
                                .map(cart->JsonUtils.toBean(cart.toString(),Cart.class))
                                .collect(Collectors.toList());

        return carts;

    }


    public void updateNum(Long skuId, Integer num) {
        //1.获取登录用户的购物车
        BoundHashOperations<String, Object, Object> boundHashOperations = getLoginUserCart();

        //2.根据skuId取出商品
        Object cartJson = boundHashOperations.get(skuId.toString());

        //3.转换为Cart对象
        Cart cart = JsonUtils.toBean(cartJson.toString(),Cart.class);

        //4.修改商品数量
        cart.setNum(num);

        //5.更新当前用户的购物车
        boundHashOperations.put(skuId.toString(),JsonUtils.toString(cart));
    }




    /**
     * 获取当前登录用户的购物车
     * @return
     */
    public BoundHashOperations<String, Object, Object> getLoginUserCart() {
        //1. 获取当前登录用户
        UserInfo loginUser = UserHolder.getUser();
        //取出用户ID
        String userId = loginUser.getId().toString();

        //2. 绑定当前登录用户的购物车
        return redisTemplate.boundHashOps(userId);
    }

    public void deleteCart(Long skuId) {
        //1.获取登录用户的购物车
        BoundHashOperations<String, Object, Object> boundHashOperations = getLoginUserCart();
        //2.删除其中指定的商品
        boundHashOperations.delete(skuId.toString());
    }
    public void deleteCart(Map<String, Object> skuOrderMap){
        //1.接收参数
        String userId = String.valueOf(skuOrderMap.get("userId"));
        List<Long> skuIdList = (List<Long>) skuOrderMap.get("skuIdList");
        BoundHashOperations<String, Object, Object> boundHashOperations = redisTemplate.boundHashOps(userId);
        //2.删除其中指定的商品
        for (Long skuId : skuIdList) {
            boundHashOperations.delete(skuId.toString());
        }

    }

    public void mergeCarts(List<Cart> carts) {
        carts.forEach(cart -> {
            this.addCart(cart);
        });
    }
}
